( function( window ) {
'use strict';
function classReg( className ) {
  return new RegExp("(^|\\s+)" + className + "(\\s+|$)");
}

var hasClass, addClass, removeClass;

if ( 'classList' in document.documentElement ) {
  hasClass = function( elem, c ) {
    return elem.classList.contains( c );
  };
  addClass = function( elem, c ) {
    elem.classList.add( c );
  };
  removeClass = function( elem, c ) {
    elem.classList.remove( c );
  };
}
else {
  hasClass = function( elem, c ) {
    return classReg( c ).test( elem.className );
  };
  addClass = function( elem, c ) {
    if ( !hasClass( elem, c ) ) {
      elem.className = elem.className + ' ' + c;
    }
  };
  removeClass = function( elem, c ) {
    elem.className = elem.className.replace( classReg( c ), ' ' );
  };
}

function toggleClass( elem, c ) {
  var fn = hasClass( elem, c ) ? removeClass : addClass;
  fn( elem, c );
}

var classie = {
  // full names
  hasClass: hasClass,
  addClass: addClass,
  removeClass: removeClass,
  toggleClass: toggleClass,
  // short names
  has: hasClass,
  add: addClass,
  remove: removeClass,
  toggle: toggleClass
};

// transport
if ( typeof define === 'function' && define.amd ) {
  // AMD
  define( classie );
} else {
  // browser global
  window.classie = classie;
}

})( window );

// 一律使用click事件，否则手机上点击其他元素时，会先触发touchstart，导致事件冒泡
eventtype = 'click';	
bodyClickFn = function(evt) {
	if( !hasParentClass( evt.target, 'st-menu' ) ) {
		resetMenu();
		document.removeEventListener( eventtype, bodyClickFn );
		document.getElementById("btnMailbox").classList.toggle("is-active");
	}
};
resetMenu = function() {
	classie.remove( container, 'st-menu-open' );
}
var container;

function hasParentClass( e, classname ) {
	if(e === document) return false;
	if( classie.has( e, classname ) ) {
		return true;
	}
	return e.parentNode && hasParentClass( e.parentNode, classname );
}

function SidebarMenuEffects(){
	function init() {
			//buttons = Array.prototype.slice.call( document.querySelectorAll( '#st-trigger-effects > button' ) ),
			var buttons = Array.prototype.slice.call(document.querySelectorAll('.pageHeaderLeft,#btnMailbox'));
			// event type (if mobile use touch events)
			buttons.forEach( function( el, i ) {
			var effect = "st-effect-2";//el.getAttribute( 'data-effect' );
			el.addEventListener( eventtype, function( ev ) {
				var panel = $(".currentPanel");
				$("[is_swiped='true']",panel).trigger("swipeRight");
				
				hideBatchMenu();
				if($(container).hasClass("st-menu-open")){
					resetMenu();
					document.removeEventListener( eventtype, bodyClickFn );
					document.getElementById("btnMailbox").classList.toggle("is-active");
				}else{
					ev.stopPropagation();
					ev.preventDefault();
					container.className = 'st-container'; // clear
					classie.add( container, effect );
					setTimeout( function() {
						classie.add( container, 'st-menu-open' );
					}, 25 );
					document.getElementById("btnMailbox").classList.toggle("is-active");
					document.addEventListener( eventtype, bodyClickFn );
				}
			});
		} );
	}
	init();
} 


$(document).ready(function(){
	container = document.getElementById('st-container' );
	SidebarMenuEffects();
});
